﻿using daonaite.core.Attribute;
using daonaite.Model.system.dictType;
using daonaite.Model.system.menu;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace daonaite.DAL.system
{
    [AutoLoad]
    public class D_dicttype
    {
        private readonly ISqlSugarClient client;

        public D_dicttype(ISqlSugarClient client)
        {
            this.client = client;
        }

        /// <summary>
        /// 查询字典类型
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<List<sys_dicttype>> select(Dto_dicttype data, RefAsync<int> total)
        {
            Expressionable<sys_dicttype> expre = Expressionable.Create<sys_dicttype>();
            expre.And(it => it.deleteFlag == "N");
            expre.AndIF(data.id != null, it => it.id == data.id);
            expre.AndIF(!string.IsNullOrEmpty(data.dictname), it => it.dictname.Contains(data.dictname));
            expre.AndIF(!string.IsNullOrEmpty(data.biaoshi), it => it.biaoshi == data.biaoshi);
            var exp = expre.ToExpression();
            return await client.Queryable<sys_dicttype>().Where(exp).ToPageListAsync(data.pageIndex, data.pageSize, total);
        }

        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<sys_dicttype> get(int id)
        {
            return await client.Queryable<sys_dicttype>().FirstAsync(x => x.id == id);
        }

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<int> add(sys_dicttype data)
        {
            return await client.Insertable(data).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<int> updateAsync(sys_dicttype data)
        {
            return await client.Updateable(data).IgnoreColumns(ignoreAllNullColumns: true)
                   .WhereColumns(it => new { it.id }).ExecuteCommandAsync();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> deleteAsync(int id)
        {
            return await client.Updateable<sys_dicttype>().SetColumns(s => s.deleteFlag == "Y").Where(it => it.id == id).ExecuteCommandAsync();
        }
    }
}
